package com.easyUploader.controller;

import com.common.mybaits.CustomPage;
import com.easyUploader.assembler.BucketAssembler;
import com.easyUploader.entity.dto.BucketDto;
import com.easyUploader.entity.dto.FileDto;
import com.easyUploader.service.IFileStorageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import com.common.result.ResponseResultAdvice;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;
import static com.easyUploader.property.LocalFileProperty.DEFAULT_SHARE_VISIT_CONTROLLER_PREFIX;



/**
 * @author 大忽悠
 * @create 2022/9/15 11:09
 */
@Validated
@RestController
@RequestMapping(DEFAULT_SHARE_VISIT_CONTROLLER_PREFIX)
@RequiredArgsConstructor
@ResponseResultAdvice
@Api(tags="共享文件访问请求")
public class ShareVisitorController {
    private final BucketAssembler bucketAssembler;
    private final IFileStorageService iFileStorageService;

    /**
     * TODO: 共享空间下暂时只有一个default桶
     */
    @ApiOperation("查询共享存储桶")
    @GetMapping
    public BucketDto shareBucket(){
        return bucketAssembler.getShareBucket();
    }

    /**
     * TODO: 共享空间下暂时只有一个default桶
     */
    @ApiOperation("查询共享桶下的文件信息")
    @GetMapping("/listShareFiles")
    public List<FileDto> listShareFiles(@ApiParam("目录路径") String dir, CustomPage customPage){
       return bucketAssembler.listShareFiles(dir,customPage);
    }

    @ApiOperation("根据文件名访问文件")
    @GetMapping("/**")
    public void visitShareByFileName(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
        iFileStorageService.visitShareByFileName(request, response);
    }

    @ApiOperation("根据文件名或者目录名下载文件")
    @GetMapping( "/download")
    public void downLoadShareFile(HttpServletRequest request, HttpServletResponse response,@RequestParam("path")@ApiParam("fileKey/filename") String relativePath) throws IOException {
        iFileStorageService.downLoadShareFile(relativePath, request, response);
    }
}
